package fourFragment.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseFragment;
import common.Constant;
import https.InterNetController;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import topic.activity.TopicDetail;
import topic.adapter.TopicAdapter;
import topic.bean.TopicGroupBean;
import view.MyPtrRefresh_LoadView;

/**
 * Created by Administrator on 2017/4/12.
 */
@SuppressLint("ValidFragment")
public class MyFocusTopic extends BaseFragment {
    private View view;
    private MyPtrRefresh_LoadView my_focus_topic_refreshLayout;
    private ListView my_focus_topic_lv;
    private final int ONREFRESN = 0;
    private final int ONLOAD = 1;
    private TopicAdapter topicAdapter;
    private ArrayList<TopicGroupBean.BlogListBean> arrayList;

    private int page = 1;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    Gson gson = new Gson();
                    TopicGroupBean topicGroupBean = gson.fromJson((String) msg.obj, TopicGroupBean.class);
                    if (topicGroupBean != null) {
                        List<TopicGroupBean.BlogListBean> expertList = topicGroupBean.getBlogList();
                        if (expertList != null) {
                            if (msg.arg1 == ONREFRESN) {
                                arrayList.clear();
                            }
                            if (expertList.size() > 0) {
                                for (int i = 0; i < expertList.size(); i++) {
                                    arrayList.add(expertList.get(i));
                                }

                            } else {
                                if (msg.arg1 == ONLOAD) {
                                    page--;
                                }
                            }
                            topicAdapter.notifyDataSetChanged();
                        }
                    }
                    break;
                case Constant.ERROR_CODE_FINISH:
                    my_focus_topic_refreshLayout.OnRefreshComplete();
                    break;
                default:
                    break;
            }
        }
    };


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.myfocus_topic, null);
        initView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        getDataFromInterNet(page = 1, ONREFRESN);
    }

    private void initView() {
        my_focus_topic_refreshLayout = (MyPtrRefresh_LoadView) view.findViewById(R.id.my_focus_topic_refreshLayout);
        my_focus_topic_lv = (ListView) view.findViewById(R.id.my_focus_topic_lv);
        my_focus_topic_refreshLayout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getDataFromInterNet(++page, ONLOAD);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getDataFromInterNet(page = 1, ONREFRESN);
            }
        });
        arrayList = new ArrayList<>();
        topicAdapter = new TopicAdapter(getMyActivity(), arrayList, new TopicAdapter.OnTopicDetailListener() {
            @Override
            public void onTopicDetailListener(int position) {
                Intent i = new Intent(getMyActivity(), TopicDetail.class);
                i.putExtra("blogid", arrayList.get(position).getBlogid());
                startActivityForResult(i, position);
            }
        });
        my_focus_topic_lv.setAdapter(topicAdapter);
    }

    private void getDataFromInterNet(int page, int flag) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("type", "topic");
        hashMap.put("page", String.valueOf(page));
        new InterNetController(getMyActivity(), Constant.ATTENTIONINFO, handler, hashMap, flag,false);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 1){
            boolean changeFocus = data.getBooleanExtra("changeFocus", false);
            if(changeFocus){
                getDataFromInterNet(page = 1, ONREFRESN);
            }
        }
    }
}